package com.qkbb.admin.kuibu.qkbb.selectphoto;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.qkbb.admin.kuibu.R;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;

/**
 * 展示界面
 * 
 * @author seven2729
 * @e-mail 731739299@qq.com
 * @version v1.0
 * @copyright 2010-2015
 * @create-time 2015年5月14日 下午2:19:56
 *
 */
public class MainActivity extends Activity {

	private static final int REQUEST_PICK = 0;
	private GridView gridview;
	private GridAdapter adapter;
	ImageView iv_main;
	private ArrayList<String> selectedPicture = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_main);
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				this).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				.diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(50)//缓存大小、个数
				.tasksProcessingOrder(QueueProcessingType.LIFO).build();
		ImageLoader.getInstance().init(config);
		gridview = (GridView) findViewById(R.id.gridview);

//		iv_main = (ImageView) findViewById(R.id.iv_main);
		adapter = new GridAdapter();
		gridview.setAdapter(adapter);

	}

	public void selectPicture(View view) {
		startActivityForResult(new Intent(this, SelectPictureActivity.class),
				REQUEST_PICK);
	}

	/**
	 * 路径回调
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			selectedPicture = (ArrayList<String>) data
					.getSerializableExtra(SelectPictureActivity.INTENT_SELECTED_PICTURE);
//			Picasso.with(getApplication()).load(new File("file://" + selectedPicture.get(1))).into(iv_main);
			adapter.notifyDataSetChanged();
		}
	}

	/**
	 * 图片适配器
	 */
	class GridAdapter extends BaseAdapter {
		LayoutParams params = new AbsListView.LayoutParams(100, 100);

		@Override
		public int getCount() {
			return selectedPicture.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = new ImageView(MainActivity.this);
				convertView.setLayoutParams(new LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
				((ImageView) convertView).setScaleType(ScaleType.CENTER_CROP);
				convertView.setLayoutParams(params);
			}
//			ImageLoader.getInstance().displayImage(
//					"file://" + selectedPicture.get(position),
//					(ImageView) convertView);
			Picasso.with(getApplication()).load(new File(selectedPicture.get(position))).resize(100,100).centerCrop().into((ImageView) convertView);
			return convertView;
		}

	}
}
